package com.ids.idtma.ftp;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;

import com.ids.idtma.IdtApplication;
import com.ids.idtma.ftp.FtpUtil.UploadProgressListener;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;

public class ProgressInputStream extends InputStream {

	private static final int TEN_KILOBYTES = 10 * 1024; // 每上传10k返回一次

	private InputStream inputStream;

	private long progress;
	private long lastUpdate;

	private boolean closed;

	private UploadProgressListener listener;
	private File localFile;

	public ProgressInputStream(InputStream inputStream, UploadProgressListener listener, File localFile) {
		this.inputStream = inputStream;
		this.progress = 0;
		this.lastUpdate = 0;
		this.listener = listener;
		this.localFile = localFile;

		this.closed = false;
	}

	@Override
	public int read() throws IOException {
		int count = inputStream.read();
		// LwtLog.d(IdtApplication.WULIN_TAG, "count:"+count);
		return incrementCounterAndUpdateDisplay(count);
	}

	@Override
	public int read(byte[] b, int off, int len) throws IOException {
		int count = inputStream.read(b, off, len);
		return incrementCounterAndUpdateDisplay(count);
	}

	@Override
	public void close() throws IOException {
		super.close();
		if (closed)
			throw new IOException("already closed");
		closed = true;
	}

	private int incrementCounterAndUpdateDisplay(int count) {
		if (count > 0)
			progress += count;
		lastUpdate = maybeUpdateDisplay(progress, lastUpdate);
		return count;
	}

	private long maybeUpdateDisplay(long progress, long lastUpdate) {
		if (progress - lastUpdate > TEN_KILOBYTES) {
			lastUpdate = progress;
			this.listener.onUploadProgress(FtpBuinessLayer.FTP_UPLOAD_LOADING, progress, this.localFile);
		}
		return lastUpdate;
	}

}
